package com.lfg.leetcode.leetcode100;


import com.lfg.leetcode.leetcode0.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class No129sumNumbers {

    public static void main(String[] args) {
        int ret = sumNumbers(TreeNode.stringToTreeNode("[1,2,3]"));
        System.out.println("[" + ret + "]");
    }


    public static int sumNumbers(TreeNode root) {
        if (null == root) {
            return 0;
        }
        ArrayList<Integer> ret = new ArrayList<>();
        getAllNum(root, ret, 0);

        int total = 0;
        for (int i : ret) {
            total += i;
        }
        return total;
    }

    public static void getAllNum(TreeNode root, List<Integer> nums, int cur) {
        int number = cur * 10 + root.val;
        if (root.left == null && root.right == null) {
            nums.add(number);
            return;
        }

        if (root.left != null) {
            getAllNum(root.left, nums, number);
        }
        if (root.right != null) {
            getAllNum(root.right, nums, number);
        }
    }
}
